package com.lb.jwitter.action;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Map; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTDMNgayTrongTuan;
import com.lb.jwitter.service.TblCTDTDMNgayTrongTuanService;
import com.lb.jwitter.service.TblCTDTDMPhong;
import com.lb.jwitter.service.TblCTDTDMPhongService;
import com.lb.jwitter.service.TblCTDTDMTietHoc;
import com.lb.jwitter.service.TblCTDTDMTietHocService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinNhomLopMonHocService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;
import com.lb.jwitter.service.TblThongTinTKBChiTiet;
import com.lb.jwitter.service.TblThongTinTKBChiTietService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblThongTinTKB;
import com.lb.jwitter.service.TblThongTinTKBService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblThongTinNhomLopMonHocEditFormAction{
    private int id; 
    public void setId(int id) {
       this.id = id;
 }

    public int getId() {
       return id;
 }

    private TblThongTinNhomLopMonHoc tblThongTinNhomLopMonHoc; 
    public TblThongTinNhomLopMonHoc getTblThongTinNhomLopMonHoc() { 
        return tblThongTinNhomLopMonHoc; 
    } 
    private Collection<TblThongTinNhomLopMonHoc> tblThongTinNhomLopMonHocs; 
    public Collection<TblThongTinNhomLopMonHoc>  getTblThongTinNhomLopMonHocs() {
         return tblThongTinNhomLopMonHocs; 
      } 

    private TblThongTinNhomLopMonHocService tblThongTinNhomLopMonHocService; 

    private Collection<TblDKMHDotDK> tblDKMHDotDKs; 
    public Collection<TblDKMHDotDK>  getTblDKMHDotDKs(){ 
              return tblDKMHDotDKs; 
     } 

    private TblThongTinTKB tblThongTinTKB;
    
    public TblThongTinTKB getTblThongTinTKB() {
		return tblThongTinTKB;
	}

	public void setTblThongTinTKB(TblThongTinTKB tblThongTinTKB) {
		this.tblThongTinTKB = tblThongTinTKB;
	}
	
	
	private TblDKMHDotDKService tblDKMHDotDKService; 

    private Collection<TblCTDTMonHoc> tblCTDTMonHocs; 
    public Collection<TblCTDTMonHoc>  getTblCTDTMonHocs(){ 
              return tblCTDTMonHocs; 
     } 

    private TblCTDTMonHocService tblCTDTMonHocService; 

    private Collection<TblThongTinTKB> tblThongTinTKBs; 
    public Collection<TblThongTinTKB>  getTblThongTinTKBs(){ 
              return tblThongTinTKBs; 
     } 

    private TblThongTinTKBService tblThongTinTKBService; 

    private Collection<TblCanBo> tblCanBos; 
    public Collection<TblCanBo>  getTblCanBos(){ 
              return tblCanBos; 
     } 

    private Collection<TblThongTinTKBChiTiet> tblThongTinTKBChiTiets; 
    public Collection<TblThongTinTKBChiTiet>  getTblThongTinTKBChiTiets(){ 
              return tblThongTinTKBChiTiets; 
     } 

    private TblThongTinTKBChiTietService tblThongTinTKBChiTietService; 

    private Collection<TblCTDTDMPhong> tblCTDTDMPhongs; 
    public Collection<TblCTDTDMPhong>  getTblCTDTDMPhongs(){ 
              return tblCTDTDMPhongs; 
     } 

    private TblCTDTDMPhongService tblCTDTDMPhongService; 

    private Collection<TblCTDTDMNgayTrongTuan> tblCTDTDMNgayTrongTuans; 
    public Collection<TblCTDTDMNgayTrongTuan>  getTblCTDTDMNgayTrongTuans(){ 
              return tblCTDTDMNgayTrongTuans; 
     } 

    private TblCTDTDMNgayTrongTuanService tblCTDTDMNgayTrongTuanService; 

    private Collection<TblCTDTDMTietHoc> tblCTDTDMTietHocs; 
    public Collection<TblCTDTDMTietHoc>  getTblCTDTDMTietHocs(){ 
              return tblCTDTDMTietHocs; 
     } 

    private TblCTDTDMTietHocService tblCTDTDMTietHocService; 

    private TblCanBoService tblCanBoService; 

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_NhomLop_MonHoc", "sua")){ 
        return "noprivilege"; 
    } 
        tblThongTinNhomLopMonHocService = (TblThongTinNhomLopMonHocService) context.getBean("tbl_ThongTin_NhomLop_MonHocClient"); 


        tblThongTinTKBService = (TblThongTinTKBService) context.getBean("tbl_ThongTin_TKBClient"); 


        tblThongTinTKBChiTietService = (TblThongTinTKBChiTietService) context.getBean("tbl_ThongTin_TKB_ChiTietClient"); 

        tblCTDTDMPhongService = (TblCTDTDMPhongService) context.getBean("tbl_CTDT_DM_PhongClient"); 

        tblCTDTDMNgayTrongTuanService = (TblCTDTDMNgayTrongTuanService) context.getBean("tbl_CTDT_DM_NgayTrongTuanClient"); 

        tblCTDTDMTietHocService = (TblCTDTDMTietHocService) context.getBean("tbl_CTDT_DM_TietHocClient"); 

        
        tblThongTinNhomLopMonHoc = tblThongTinNhomLopMonHocService.gettblThongTinNhomLopMonHoc(new Long(id)); 

        tblThongTinTKB = tblThongTinNhomLopMonHoc.getTKBId();
        
        

        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        cons.clear(); 
           if (id != 0) 
           { 
               ConditionReport con = new ConditionReport(); 
               con.setCol("TKBId.id"); 
               con.setValue1(tblThongTinNhomLopMonHoc.getTKBId().getId()); 
               con.setOperator("eq"); 
               cons.add(con); 
           } 
           { 
               ConditionReport con = new ConditionReport(); 
               con.setCol("ngayHocId.STT"); 
               con.setValue1("asc"); 
               con.setOperator("order"); 
               cons.add(con); 
           } 
           { 
               ConditionReport con = new ConditionReport(); 
               con.setCol("tietBatDauId.STT"); 
               con.setValue1("asc"); 
               con.setOperator("order"); 
               cons.add(con); 
           } 
           tblThongTinTKBChiTiets = tblThongTinTKBChiTietService.findtblThongTinTKBChiTietByCondition(cons, 0, 0); 
//System.out.println(tblThongTinTKBChiTiets.size());
              tblCTDTDMNgayTrongTuans =       tblCTDTDMNgayTrongTuanService.findAlltblCTDTDMNgayTrongTuan(0, 0); 
              tblCTDTDMTietHocs =       tblCTDTDMTietHocService.findAlltblCTDTDMTietHoc(0, 0); 
              tblCTDTDMPhongs = tblCTDTDMPhongService.findAlltblCTDTDMPhong(0, 0);

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_ThongTin_NhomLop_MonHoc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }
 	public String tuanHocString(Integer tuanHoc)
	{
		String res = "";
		if(tuanHoc == null){
			tuanHoc = 0;
		}
		
		List<String> tuanHocs = new ArrayList<String>();
	    String s = Integer.toBinaryString(tuanHoc);
	    tuanHocs = new ArrayList<String>(Arrays.asList(s.split("(?!^)")));
	    int delta = 26 - tuanHocs.size();
     for(int i = 0; i < tuanHocs.size(); i++){
     	res += tuanHocs.get(i).equalsIgnoreCase("1") ? ((i+1+delta)%10) : "-";
     }
     if(tuanHocs.size() < 26){
     	for(int j = tuanHocs.size(); j < 26; j++){
     		res = "-" + res;
     	}
     } 
     return res;
	}
 	
 	public TblThongTinNhomLopMonHocEditFormAction getThis(){
 		return this;
 	}
 } 
 
